package com.lu.servlet;

import com.lu.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "LoginOutServlet",urlPatterns = {"/LoginOut"})
public class LoginOutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter out = resp.getWriter();

        List<User> currentUserList = (List<User>) req.getServletContext().getAttribute("currentUserList");
        User currentUser = (User) req.getSession().getAttribute("currentUser");
        currentUserList.remove(currentUser);
        req.getSession().removeAttribute("currentUser");
        resp.sendRedirect("index.jsp");

        out.flush();
        out.close();
    }
}
